<template>
  <div>
    <div style="padding-top: 20px;float: left;padding-left: 10px;">
      <h3 v-if="initStatus">请输入查询内容，以便展示人物信息...</h3>
      <h3 style="color: green;" v-if="loadingStatue">Loading...</h3>
      <h3 v-if="errorMsg != null">{{errorMsg}}</h3>
      <h3 v-if="nullResult != null">{{nullResult}}</h3>
    </div>
    <div id="contentDiv" v-for="(user,index) in users" :key="index">
      <a :href="user.url" target="_blank">
        <img :src="user.image" style="width: 150px;">
      </a>
      <p><a :href="user.url" target="_blank">{{user.name}}</a></p>
    </div>
  </div>
</template>

<script>
  import axiox from 'axios'
  import PubSub from 'pubsub-js'

  export default {
    name: "resultView",
    data() {
      return {
        users: "",
        initStatus: true,
        loadingStatue: false,
        errorMsg: "",
        nullResult: ""
      }
    },
    mounted() {

      PubSub.subscribe("search", (msg, searchCentent) => {
        this.users = "";
        this.initStatus = false;
        this.loadingStatue = true;
        const url = 'https://api.github.com/search/users?q=' + searchCentent;
        axiox.get(url).then(
          response => {
            let data = response.data;
            let items = data.items;
            this.users = items.map(item => ({
              url: item.html_url,
              name: item.login,
              image: item.avatar_url
            }))
            this.loadingStatue = false;
            if (this.users == null || this.users.length <= 0) {
              this.nullResult = "未查询到匹配条件的任务信息..."
            }
          }).catch(error => {
          this.loadingStatue = false;
          this.errorMsg = "request fail，case >>> " + error;
        })
      })
    }
  }
</script>

<style scoped>

  #contentDiv {
    float: left;
    width: 10%;
    margin: 10px 0px;
    border: 1px solid silver;
    padding-top: 10px
  }

</style>
